package com.syy.spider.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.syy.spider.dao.UserMapper;
import com.syy.spider.pojo.NoticeDisplay;
import com.syy.spider.pojo.NoticeGet;
import com.syy.spider.pojo.User;
import com.syy.spider.service.OtherInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OtherInfoService otherInfoService;
    @RequestMapping(value = "/getUserName")
    public void getUserName(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        ObjectMapper objectMapper = new ObjectMapper();
        if (user!=null){
            try {
                objectMapper.writeValue(response.getOutputStream(),user);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                objectMapper.writeValue(response.getOutputStream(),100);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/getNotices")
    @ResponseBody
    public List<NoticeDisplay> getNotices(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始查询公告！");
        return otherInfoService.getNotice();
    }

    @RequestMapping("/changePwd")
    public String toChange(){
        return "pages/changePwd";
    }

    @RequestMapping(value = "/jinyan",method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public boolean jinyan(@RequestBody Map<String, Object> params){
        int id = (int)params.get("id");
        User user = userMapper.selectUserById(id);
        int a = 3;
        if (user.getCom_aut()==0){
            a = userMapper.updateAut(id,1);
            userMapper.updateJuBaoshu(id);
        }else {
            a = userMapper.updateAut(id,0);
        }
        return a == 1;
    }
}
